package com.zoe.onelink.business.util.ws;


import cn.hutool.core.thread.ThreadUtil;
import com.zoe.onelink.core.async.OnelinkContextTaskDecorator;

import java.util.concurrent.ExecutorService;

/**
 * <p>标题: 请求第三方webService接口帮助类</p>
 * <p>描述: 同步、异步</p>
 * <p>版权: Copyright (c) 2018</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: cxy
 * @date: 2018/5/4
 */
public class WSRequestHelper {

    /**
     * 默认执行写入线程数
     */
    private static final int DEFAULT_WRITE_THREADS = 10;

    /**
     * 线程池
     */
    private static ExecutorService pool = ThreadUtil.newExecutor(DEFAULT_WRITE_THREADS);

    private static OnelinkContextTaskDecorator contextCopyingDecorator = OnelinkContextTaskDecorator.getInstance();

    /**
     * 执行调用(同步)
     *
     * @param wsUrl      url地址
     * @param methodName 方法名称
     * @param params     参数集
     * @return String 返回信息
     * @throws Exception 异常信息
     */
    public static Object[] doSyn(String wsUrl, String methodName, Object... params) throws Exception {
        return WSRequestUtil.doRequest(wsUrl, methodName, params);
    }

    /**
     * 执行调用(同步)
     *
     * @param wsUrl       url地址
     * @param methodName  方法名称
     * @param readTimeOut 超时时间
     * @param params      参数集
     * @return String 返回信息
     * @throws Exception 异常信息
     */
    public static Object[] doSyn(String wsUrl, String methodName, int readTimeOut, Object... params) throws Exception {
        return WSRequestUtil.doRequest(wsUrl, methodName, readTimeOut, params);
    }

    /**
     * 执行调用(异步)
     *
     * @param wsUrl      url地址
     * @param methodName 方法名称
     * @param params     参数集
     */
    public static void doAsyn(String wsUrl, String methodName, Object... params) throws Exception {
        pool.submit(contextCopyingDecorator.decorate(new AsynTask(wsUrl, methodName, params)));
    }

    /**
     * 执行调用(异步)
     *
     * @param wsUrl       url地址
     * @param methodName  方法名称
     * @param readTimeOut 超时时间
     * @param params      参数集
     */
    public static void doAsyn(String wsUrl, String methodName, int readTimeOut, Object... params) throws Exception {
        pool.submit(contextCopyingDecorator.decorate(new AsynTask(wsUrl, methodName, readTimeOut, params)));
    }

    /**
     * 异步调用任务
     */
    private static class AsynTask implements Runnable {
        // url地址
        private String wsUrl;
        // 方法名称
        private String methodName;
        // 参数集
        private Object[] params;
        // 超时时间
        private int readTimeOut = 0;

        AsynTask(String wsUrl, String methodName, Object... params) {
            this.wsUrl = wsUrl;
            this.methodName = methodName;
            this.params = params;
        }

        AsynTask(String wsUrl, String methodName, int readTimeOut, Object... params) {
            this.wsUrl = wsUrl;
            this.methodName = methodName;
            this.readTimeOut = readTimeOut;
            this.params = params;
        }

        @Override
        public void run() {
            try {
                if (0 == readTimeOut) {
                    WSRequestUtil.doRequest(wsUrl, methodName, params);
                } else {
                    WSRequestUtil.doRequest(wsUrl, methodName, readTimeOut, params);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
